/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;
import javax.swing.JPanel;
import weka.core.*;
import weka.classifiers.evaluation.*;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;

/**
 *
 * @author kevin
 */
public class ChartFramePanel {

    private ThresholdCurve tc;
    private Instances rocdata;
    private double[] TPR;
    private double[] FPR;
    protected ChartPanel chart;
    /** The index of the target class */
    protected int m_ClassIndex = 0;
    /*
     * @param predictions
     * @throws Exception
     */

    public ChartFramePanel(FastVector predictions,int index) throws Exception {

        m_ClassIndex = index;
        if(predictions != null){
            InitializeData(predictions);
            chart = CreateChartPanel(FPR, TPR);
        }
        else{
            TPR = new double[]{0};
            FPR = new double[]{0};
            chart = CreateChartPanel(FPR,TPR);
        }
        
    }
    public ChartFramePanel(double[][] curveData) throws Exception{
        if(curveData != null){
            chart = CreateChartPanel(curveData[0],curveData[1]);
        }
    }

    /**
     * Get the chart panel
     * @return
     */
    public ChartPanel getPanel(){
        return this.chart;
    }

    /**
     *
     * @param predictions
     */
    private void InitializeData(FastVector predictions) {
        CreateDataFromPredictions(predictions);
        Vector v = new Vector();
        for (int c = 0; c < FPR.length; c++) {
            double[] temp = new double[2];
            temp[0] = TPR[c];
            temp[1] = FPR[c];
            v.add(temp);
        }
        Comparator cmp = new MyCompare();
        Collections.sort(v, cmp);
        for (int i = 0; i < v.size(); i++) {
           TPR[i] = ((double[]) v.elementAt(i))[0];
           FPR[i] = ((double[]) v.elementAt(i))[1];
        }
    }

    private void CreateDataFromPredictions(FastVector predictions) {
        tc = new ThresholdCurve();
        rocdata = tc.getCurve(predictions, m_ClassIndex);
        TPR = rocdata.attributeToDoubleArray(4);
        FPR = rocdata.attributeToDoubleArray(5);
        System.out.println("FPR "+"    TPR");
        for(int i=0; i<TPR.length;i++)
        {
            System.out.println(FPR[i]+"   "+TPR[i]);
        }
    }

    class MyCompare implements Comparator {
        //对（FPR，TPR）数据进行排序处理

        public int compare(Object a, Object b) {
            double[] aa = (double[]) a;
            double[] bb = (double[]) b;
            if (aa[0] * aa[0] + aa[1] * aa[1] < bb[0] * bb[0] + bb[1] * bb[1]) {
                return -1;
            } else if (aa[0] * aa[0] + aa[1] * aa[1] > bb[0] * bb[0] + bb[1] * bb[1]) {
                return 1;
            } else {
                return 0;
            }
        }
    }

    private ChartPanel CreateChartPanel(double[] fpr, double[] tpr) throws Exception {
        GraphicPanel gp = new GraphicPanel();
        gp.createDataset(tpr, fpr);
        JFreeChart jfreechart = gp.createChart();
        ChartPanel chartpanel = new ChartPanel(jfreechart);
        return chartpanel;
        //ChartPanel
        /*
        ThresholdVisualizePanel vmc = new ThresholdVisualizePanel();
        vmc.setROCString("(Area under ROC = "+Utils.doubleToString(tc.getROCArea(rocdata), 4)+")");
        vmc.setName(rocdata.relationName());
        PlotData2D tempd = new PlotData2D(rocdata);
        tempd.setPlotName(rocdata.relationName());
        tempd.addInstanceNumberAttribute();
        //////////////specify which points are connected/////////
        boolean[] cp = new boolean[rocdata.numInstances()];
        for(int i=1;i<cp.length;i++)
        cp[i]=true;
        tempd.setConnectPoints(cp);

        //add plot
        vmc.addPlot(tempd);

        ///////////////////
        final JFrame jf= new JFrame("Weka Classifier Visualize : "+vmc.getName());
        jf.setSize(500,400);
        jf.getContentPane().setLayout(new BorderLayout());
        jf.getContentPane().add(vmc,BorderLayout.CENTER);
        jf.addWindowListener(new java.awt.event.WindowAdapter() {
        @Override
        public void windowClosing(java.awt.event.WindowEvent e){
        jf.dispose();
        }
        });
        jf.setVisible(true);
         *
         */
    }
}
